package com.xoa.service.crm.impl;

import com.xoa.dao.crm.CustomerMapper;
import com.xoa.dao.crm.LinkmanMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.crm.CustomerWithBLOBs;
import com.xoa.model.crm.Linkman;
import com.xoa.model.users.Users;
import com.xoa.service.crm.LinkManService;
import com.xoa.util.CookiesUtil;
import com.xoa.util.common.session.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class LinkManServiceImpl  implements LinkManService {

    @Autowired
    private LinkmanMapper linkmanMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private UsersMapper usersMapper;

    /**
     * 根据客户id查询联系人
     */
    @Override
    public List<Linkman> selectBylinkman(Integer customerId) {
        List<Linkman> linkmen = linkmanMapper.selectBylinkman(customerId);
        //创建一个list集合
        List<Linkman> linkmenMe = new ArrayList<>();

        for (Linkman lin:linkmen) {
            if(lin.getIsDeleted() == 0){
                linkmenMe.add(lin);
            }
        }
        return linkmenMe;

    }

    /*
        * 添加联系人
        * */
    @Override
    public int addLinkMan(Linkman linkman,Integer customerId) {
        //创建时间
        linkman.setCreateTime(new Date());

        //获取登录用户
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionCookie);
        //创建人
        linkman.setCreator(users.getUserId());
        linkman.setCustomerId(customerId);

        return  linkmanMapper.insertSelective(linkman);
    }

    /**
     * 根据客户id回显联系人
     */
    @Override
    public Linkman selectlinkmanById(Integer customerId) {
        return linkmanMapper.selectcustomerById(customerId);
    }
    @Override
    public Linkman selectcustomerByLId(Integer customerId) {
        Linkman linkman = linkmanMapper.selectcustomerByLId(customerId);
        String creatorNew = linkman.getCreatorNew();
        String usernameByUserId = usersMapper.getUsernameByUserId(creatorNew);
        linkman.setCreatorNew(usernameByUserId);

        return linkman;
    }

    /**
     * 根据联系人id回显联系人
     */
    @Override
    public Linkman selectByPrimaryKey(Integer linkmanId) {
        return linkmanMapper.selectByPrimaryKey(linkmanId);
    }



    /**
     * 编辑联系人
     */
    @Override
    public int updateLinkMan(Linkman linkman) {
        //创建时间
        linkman.setUpdateTime(new Date());

        //获取登录用户
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionCookie);
        //创建人
        linkman.setUpdator(users.getUserId());

        return linkmanMapper.updateByPrimaryKeyCustomerId(linkman);
    }

    /**
     * 删除联系人
     */
    @Override
    public int deleteLinkMan(Integer linkmanId) {
        //获取登录用户
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionCookie);

        Linkman linkman = linkmanMapper.selectByPrimaryKey(linkmanId);
        linkman.setDeleteUser(users.getUserId());
        linkman.setDeleteTime(new Date());
        linkman.setIsDeleted(Byte.valueOf("1"));

        return linkmanMapper.updateByPrimaryKeyCustomerId(linkman);
    }
}

